package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.demo.pojo.ErrorLog;
import com.my.demo.pojo.FeedbackLog;
import com.my.demo.pojo.LoginLog;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.UploadLog;
import com.my.demo.service.ErrorLogService;
import com.my.demo.service.FeedbackLogService;
import com.my.demo.service.LoginLogService;
import com.my.demo.service.UploadLogService;

import java.util.Map;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LogController {
  @Autowired
  FeedbackLogService feedbackLogService;
  @Autowired
  LoginLogService loginLogService;
  @Autowired
  ErrorLogService errorLogService;
  @Autowired
  UploadLogService uploadLogService;


  /*
   * 用户的反馈日志提交:
   * 要获取当前用户的id:用户登录后将当前用户的id放在model中，属性名为user_id
   * */
  @ResponseBody
  @CrossOrigin
  @PostMapping("/feedback_log_upload")
  public boolean feedbackLogUpload(@RequestBody Map<String, String> map,
                                   HttpSession session) {
    Integer user_id = (Integer) session.getAttribute("id");//获取当前的用户id
    System.out.println("id:" + user_id);
    String feedback_data = map.get("feedback_data");
    FeedbackLog feedbackLog = new FeedbackLog(user_id, feedback_data);
    return feedbackLogService.save(feedbackLog);
  }

  /*
   * 用户的反馈日志展示：
   * 传给前台一页反馈日志的信息
   * */
  @ResponseBody
  @PostMapping("/print_feedback_log")
  @CrossOrigin
  public Result<Page<FeedbackLog>> feedbackLogPrint(@RequestBody Map<String, String> map
  ) {
    Result<Page<FeedbackLog>> result = new Result<>();
    result.setSuccess(false);
    result.setMsg(null);
    Integer pn = Integer.parseInt(map.get("pn"));
    System.out.println("pn:" + pn);
    QueryWrapper<FeedbackLog> queryWrapper = new QueryWrapper<FeedbackLog>();
    Page<FeedbackLog> feedbackLogPage = null;
    Page<FeedbackLog> page = null;
    try {
      queryWrapper.orderByDesc("time");
      feedbackLogPage = new Page<FeedbackLog>(pn, 10);
      page = feedbackLogService.page(feedbackLogPage, queryWrapper);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    result.setSuccess(true);
    result.setMsg("反馈日志获取成功");
    result.setDetail(page);
//        result.setDetail(list);
    return result;
  }

  @ResponseBody
  @PostMapping("/print_login_log")
  @CrossOrigin
  public Result<Page<LoginLog>> loginLogPrint(@RequestBody Map<String, String> map) {
    Result<Page<LoginLog>> result = new Result<>();
    result.setSuccess(false);
    result.setMsg(null);
    Integer pn = Integer.parseInt(map.get("pn"));
    System.out.println("pn:" + pn);
    QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<LoginLog>();
    Page<LoginLog> loginLogPage = null;
    Page<LoginLog> page = null;
    try {
      queryWrapper.orderByDesc("login_time");
      loginLogPage = new Page<LoginLog>(pn, 10);
      page = loginLogService.page(loginLogPage, queryWrapper);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    result.setSuccess(true);
    result.setMsg("登录日志获取成功");
    result.setDetail(page);
    return result;
  }


  @ResponseBody
  @PostMapping("/print_error_log")
  @CrossOrigin
  public Result<Page<ErrorLog>> errorLogPrint(@RequestBody Map<String, String> map) {
    Result<Page<ErrorLog>> result = new Result<>();
    result.setSuccess(false);
    result.setMsg(null);
    Integer pn = Integer.parseInt(map.get("pn"));
    System.out.println("pn:" + pn);
    QueryWrapper<ErrorLog> queryWrapper = new QueryWrapper<ErrorLog>();
    Page<ErrorLog> errorLogPage = null;
    Page<ErrorLog> page = null;
    try {
      queryWrapper.orderByDesc("error_time");
      errorLogPage = new Page<ErrorLog>(pn, 10);
      page = errorLogService.page(errorLogPage, queryWrapper);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    result.setSuccess(true);
    result.setMsg("错误日志获取成功");
    result.setDetail(page);
    return result;
  }

  @ResponseBody
  @PostMapping("/print_upload_log")
  @CrossOrigin
  public Result<Page<UploadLog>> uploadLogPrint(@RequestBody Map<String, String> map) {
    Result<Page<UploadLog>> result = new Result<>();
    result.setSuccess(false);
    result.setMsg(null);
    Integer pn = Integer.parseInt(map.get("pn"));
    System.out.println("pn:" + pn);
    QueryWrapper<UploadLog> queryWrapper = new QueryWrapper<UploadLog>();
    Page<UploadLog> uploadLogPage = null;
    Page<UploadLog> page = null;
    try {
      queryWrapper.orderByDesc("upload_time");
      uploadLogPage = new Page<UploadLog>(pn, 10);
      page = uploadLogService.page(uploadLogPage, queryWrapper);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    result.setSuccess(true);
    result.setMsg("上传日志获取成功");
    result.setDetail(page);
    return result;
  }
}